package com.bw.msg.mvvm.viewmodel;

import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.entity.UserEntity;
import com.bw.msg.adapter.MsgAdapter;
import com.bw.msg.entity.MsgEntity;
import com.bw.msg.mvvm.model.MsgModel;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/19 10:15
 */
public class MsgViewModel extends BaseViewModel<MsgModel> {

    public BaseQuickAdapter adapter = new MsgAdapter();
    @Override
    public void oncreate() {
        super.oncreate();
        requestMsg();
    }

    @Override
    public void onDestory() {
        super.onDestory();
    }

    public void requestMsg(){
        HashMap<String, Object> hashMap = new HashMap<>();
        List<UserEntity> list = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
        hashMap.put("id",list.get(0).getUser_id());
        m.requestMsg(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity instanceof MsgEntity){
            MsgEntity msgEntity = (MsgEntity) baseEntity;
            adapter.setNewInstance(msgEntity.getData());
        }
    }
}
